<template>
  <el-drawer
    :visible.sync="drawer"
    direction="rtl"
    size="50%"
    :with-header="false"
    :width="700"
  >
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img
              class="drawer_t_l_img"
              src="../../../../../static/skin/herodoo/main/platform/home/img/piliangoumai.png"
              alt=""
            />
            修改司机信息
          </div>
          <img
            class="drawer_t_r"
            src="../../../../../static/skin/herodoo/main/platform/home/img/guanbi.png"
            alt=""
            @click="drawer = false"
          />
        </div>
      </div>
      <div class="drawer_content">
        <div class="title">司机信息：</div>
        <div class="form-item">
          <div class="label">所属公司：</div>
          <el-select v-model="form.CompanyID" placeholder="请选择所属公司" @change="changeCompany">
            <el-option v-for="item in CompanyList" :key="item.ID" :label="item.Name" :value="item.ID">
            </el-option>
          </el-select>
        </div>
        <div class="form-item">
          <div class="label">所属部门：</div>
          <el-select v-model="form.DepartmentID" placeholder="请选择所属部门" @change="changeDepartment"
            :no-data-text="epartmentList.length == 0 && form.CompanyID ? '该公司暂无部门' : '请先选择公司名称'">
            <el-option v-for="item in epartmentList" :key="item.ID" :label="item.Name" :value="item.ID">
            </el-option>
          </el-select>
        </div>
        <div class="form-item">
          <div class="label">所属岗位：</div>
          <el-select v-model="form.RoleID" placeholder="请选择所属岗位" @change="changePost"
            :no-data-text="postList.length == 0 && form.CompanyID ? '该公司暂无岗位' : '请先选择公司名称'">
            <el-option v-for="item in postList" :key="item.ID" :label="item.Name" :value="item.ID">
            </el-option>
          </el-select>
        </div>
        <div class="form-item">
          <div class="label">姓名：</div>
          <input class="input" v-model="form.Name" placeholder="请输入司机姓名" />
        </div>
        <div class="form-item">
          <div class="label">工号：</div>
          <input class="input" v-model="form.Code" placeholder="请输入司机工号" />
        </div>
        <div class="form-item">
          <div class="label">联系方式：</div>
          <input class="input" v-model="form.Mobile" placeholder="请输入司机联系方式" />
        </div>
        <div class="form-item">
          <div class="label">性别：</div>
          <el-select v-model="form.Sex" placeholder="请选择" @change="changeSex">
            <el-option
              v-for="item in sexList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="form-item">
          <div class="label">身份证号：</div>
          <input class="input" v-model="form.IdentityNumber" placeholder="请输入司机身份证号" />
        </div>
        <!-- <div class="form-item">
          <div class="label">说明：</div>
          <textarea class="textarea" v-model="form.desc" placeholder="请输入备注内容" ></textarea>
        </div> -->

        <div v-if="type == 2">
          <div class="title">车辆信息：</div>
          <div class="cat-info" v-for="(catInfo, index) in catList" :key="catInfo.id">
            <div class="title">
              <!-- <div class="delete-box" v-if="catList.length > 1" @click="deleteCat(catInfo, index)"></div> -->
              车辆{{index+1}}：
            </div>
            <div class="form-item" >
              <div class="label">车牌号：</div>
              <div class="desc">{{ catInfo.Code }}</div>
            </div>
            <div class="form-item" >
              <div class="label">车辆品牌：</div>
              <div class="desc">{{ catInfo.BrandID }}</div>
            </div>
            <div class="form-item" >
              <div class="label">车系：</div>
              <div class="desc">{{ catInfo.SystemID }}</div>
            </div>
            <div class="form-item" >
              <div class="label">车型：</div>
              <div class="desc">{{ catInfo.TypeID }}</div>
            </div>
          </div>
          <!-- <div class="form-item">
            <div class="label">添加车辆信息：</div>
            <div class="add-driver" @click="handleAddFormCat">
              <i class="el-icon-plus"></i>
              <span>点击添加车辆</span>
            </div>
          </div> -->
          <div class="form-item">
            <div class="label">创建人：</div>
            <div class="desc">{{ form.CreateUserName }}</div>
          </div>
          <div class="form-item">
            <div class="label">创建日期：</div>
            <div class="desc">{{ form.CreateDate }}</div>
          </div>
        </div>
      </div>
      <div class="drawer_footer">
        <button class="footer-btn" @click="handleUpdate">保存</button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
export default {
  name: "cat_dialog",
  props: {
    type: {
      type: [String, Number],
      default: 1
    },
    messageList: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      catList: [],
      form: {},
      sex: "",
      sexList: [
        {
          label: '男',
          value: 1
        },
        {
          label: '女',
          value: 0
        }
      ],
      CompanyList: [],
      epartmentList: [],
      postList: [],
    }
  },
  watch: {
    drawer: {
      handler(v) {
        if (v) {
          this.getCompanySelect()
          this.epartmentList = []
          this.postList = []
          if(Object.keys(this.messageList).length) {
            this.form = this.messageList
            this.getCatList()
            if(this.form.CompanyID) {
              this.getDepartment()
              this.getPostList()
            }
          } else if(this.type == 1) {
            this.form = {
              CompanyID: '',
              DepartmentID: '',
              RoleID: '',
              Name: '',
              Code: '',
              Mobile: '',
              Sex: 1,
              IdentityNumber: '',
            }
          }
        }
      }
    },

  },
  methods: {
    drawerOp() {
      this.drawer = true;
    },
    changeCompany(val) {
      this.form.CompanyID = val
      this.form.DepartmentID = null
      this.form.RoleID = null
      this.getDepartment()
      this.getPostList()
    },
    changeDepartment(val) {},
    changePost(val) {},

    // 获取公司列表
    getCompanySelect() {
      let o = this
      o.CompanyList = []
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 31,
          cellData: [
            { name: "ID" },	// 产品ID
            { name: "Name" },	// 合同-产品名称
          ],
          pageData: {
            rowSize: 777
          },
        },
        function (data) {
          if (data.status.code == 200) {
            if (o.octopus.passport.authorization.company != 1) {
              let index = data.content.queryData.findIndex((item, index) => {
                return item.ID == o.octopus.passport.authorization.company
              })
              o.CompanyList.push(data.content.queryData[index])
              o.form.CompanyID = o.CompanyList[0].ID
              o.getDepartment()
              o.getPostList()
            }else{
              o.CompanyList = data.content.queryData
            }
          } else {
            o.octopus.tips.error(data);
          }
        })
    },

    // 获取部门列表
    getDepartment() {
      let o = this
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 62,
          cellData: [
            {name: "Code"},	// 部门编号
            {name: "CompanyID", filterKey: '=', filterValue: this.form.CompanyID},	// 所属公司
            {name: "ID"},	// ID
            {name: "Name"},	// 部门名称
            {name: "ParentID"},	// 父ID
          ],
          pageData: {
            rowSize: 777
          },
        },
        function (data) {
          if (data.status.code == 200) {
            o.epartmentList = data.content.queryData
          } else {
            o.octopus.tips.error(data);
          }
        })
    },

    // 获取岗位列表
    getPostList() {
      let o = this
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 10,
          cellData: [
            {name: "CompanyID", filterKey: '=', filterValue: this.form.CompanyID},	// 所属公司
            {name: "ID"},	// ID
            {name: "Name"},	// 岗位名称
          ],
          pageData: {
            rowSize: 777
          },
        },
        function (data) {
          if (data.status.code == 200) {
            o.postList = data.content.queryData
          } else {
            o.octopus.tips.error(data);
          }
        })
    },
    // 获取车辆信息
    getCatList(){
      this.octopus.request.api.post("/Octopus/Engine/List",{
        id: 1253,
        cellData: [
          {name: "BrandID"},
          {name: "Code"},
          {name: "CompanyID"},
          {name: "CreateTime"},
          {name: "CreateUserID"},
          {name: "Driver"},
          {name: "ID"},
          {name: "Mobile"},
          {name: "SystemID"},
          {name: "TypeID"},
          {name: "UseCount"},
        ],
        paramData: {
          DriverID: 267298
        }
      },
      (data) => {
        if (data.status.code == 200) {
          this.catList = data.content.queryData
        } else {
          this.octopus.tips.error(data);
        }
      })
    },

    handleAddFormCat() {
      const newId = this.catList.length + 1;
      // 创建一个与数组中第一个对象相同结构的新对象
      const obj = {
        id: newId,
        name: '',
      };
      // 将新对象添加到数组中
      this.catList.push(obj)
    },
    deleteCat(item, index) {
      this.catList.splice(index, 1)
    },
    handleUpdate() {
      if(!this.verify()) {
        return
      }
      this.form.Sex == '男' && (this.form.Sex = 1)
      this.form.Sex == '女' && (this.form.Sex = 0)
      let o = this
      let reqType = "UPDATE"
      if(this.type == 1) {
        reqType = "CREATE"
        o.form.TypeID = 2
        o.form.RealName = o.form.Name
        o.form.NickName = o.form.Name
        o.form.Password = o.form.Mobile.slice(-6)
        o.form.CreateUserID = o.octopus.passport.id
        o.form.CreateDate = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0, 8)
      }
      this.octopus.request.api.post("/Octopus/Engine/Item", {
        id: 1,
        type: reqType,
        value: o.form.ID,
        cellData: [
          {name: "CompanyID", defaultValue: o.form.CompanyID},	// 公司ID
          {name: "DepartmentID", defaultValue: o.form.DepartmentID},	// 部门ID
          {name: "RoleID", defaultValue: o.form.RoleID},	// 所属岗位
          {name: "TypeID", defaultValue: o.form.TypeID},	// 人员类型
          {name: "Name", defaultValue: o.form.Name},	// 账号名/手机号
          {name: "NickName", defaultValue: o.form.NickName},	// 昵称
          {name: "RealName", defaultValue: o.form.RealName},	// 真实名
          {name: "IdentityNumber", defaultValue: o.form.IdentityNumber},	// 身份证号
          {name: "Sex", defaultValue: o.form.Sex},	// 性别
          {name: "Mobile", defaultValue: o.form.Mobile},	// 手机号
          {name: "Password", defaultValue: o.form.Password},	// 密码
          {name: "Code", defaultValue: o.form.Code},	// 工号
          {name: "CreateUserID", defaultValue: o.form.CreateUserID},	// 创建人
          {name: "CreateDate", defaultValue: o.form.CreateDate},	// 创建时间
        ]
      }, function(data) {
        if (data.status.code === 200) {
          o.$message.success('修改成功')
          o.$emit('refresh')
          o.drawer = false
        } else {
          o.octopus.tips.error(data);
        }
      })
    },

    verify() {
      const phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      const userNumberReg = /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/
      if(this.form.CompanyID == "" || this.form.CompanyID == null) {
        this.$message.error('请选择司机所属公司')
        return false
      }
      if(this.form.DepartmentID == "" || this.form.DepartmentID == null) {
        this.$message.error('请选择司机所属部门')
        return false
      }
      if(this.form.RoleID == "" || this.form.RoleID == null) {
        this.$message.error('请选择司机所属岗位')
        return false
      }
      if(this.form.Name == "") {
        this.$message.error('请输入司机姓名')
        return false
      }
      if(this.form.Code == "") {
        this.$message.error('请输入司机工号')
        return false
      }
      if(this.form.Mobile == "" || !phoneReg.test(this.form.Mobile)) {
        this.$message.error('请正确输入司机联系方式')
        return false
      }
      if((this.form.IdentityNumber != null && this.form.IdentityNumber != "")  && !userNumberReg.test(this.form.IdentityNumber)) {
        this.$message.error('请正确输入司机身份证号')
        return false
      }
      return true
    },
  },
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
  display: flex;
  align-items: center;
}
.cat-info {
  margin: 2rem 0 2rem 2em;
}

.cat-info .delete-box {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #f88c52;
  position: relative;
  margin-right: 6px;
}
.cat-info .delete-box::before {
  position: absolute;
  content: '';
  width: 60%;
  height: 2px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  z-index: 1;
}

.form-item {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}
.form-item .label {
  width: 25%;
  font-size: 1.6rem;
}

.form-item .input {
  width: 100%;
  height: 5rem;
  line-height: 5rem;
  padding: 0 2rem;
  border: 1px solid #F3E8E2;
  border-radius: 1rem;
}
>>>.form-item .el-select {
  width: 100%;
  height: 4.5rem;
  line-height: 5rem;
}
>>>.form-item .el-select .el-input {
  width: 100%;
  height: 4.5rem;
}

>>>.form-item .el-select .el-input__inner {
  cursor: pointer;
  padding-right: 35px;
  height: 100%;
  font-size: 1.8rem;
}
>>>.el-input__icon {
  line-height: 3.5rem;
}
.form-item .textarea {
  width: 100%;
  line-height: 5rem;
  padding: 0 2rem;
  border: 1px solid #F3E8E2;
  border-radius: 1rem;
}

.form-item .add-driver {
  height: 4.5rem;
  line-height: 4.5rem;
  border: 1px dashed #F87338;
  display: inline-block;
  padding: 0 1.5rem;
  cursor: pointer;
  border-radius: .5rem;
}

.form-item .add-driver i {
  color: #F87338;
  font-weight: 700;
  margin-right: .5rem;
}
.drawer_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}

.drawer_footer .footer-btn {
  line-height: 4rem;
  background-color: #F78C51;
  color: #fff;
  margin: 0 1rem;
  padding: 0 3rem;
  border-radius: .5rem;
  cursor: pointer;
  border: none;
}
</style>